﻿//INSTANT C# NOTE: Formerly VB project-level imports:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Linq;
using System.Xml.Linq;
using System.Threading.Tasks;

namespace Battery_Notification
{
	public partial class Form1
	{
		private bool InstanceFieldsInitialized = false;

		internal Form1()
		{
			if (!InstanceFieldsInitialized)
			{
				InitializeInstanceFields();
				InstanceFieldsInitialized = true;
			}

			InitializeComponent();
		}

			private void InitializeInstanceFields()
			{
				/*percent = power.BatteryLifePercent * 100;
				x = power.PowerLineStatus;*/
			}

		private void Form1_Load(object sender, EventArgs e)
		{
		}

		//Declare High and Low values
		private float Hi;
		private float Lo;

		//Powerstates
		private PowerStatus power = SystemInformation.PowerStatus;
        //public byte BatteryLifePercent { get; set; } 
		private float x = (int)SystemInformation.PowerStatus.PowerLineStatus;
        private float BatteryLife = SystemInformation.PowerStatus.BatteryLifePercent * 100;


        //Timer for checking stuff in intervals
        private long elapsedTime = 0;
        private long elapsedTimeHolding = 31536000;



        //' Test functions below


        //' Test functions above

        //Minimize to tray
        private void Form1_Resize(object sender, System.EventArgs e)
		{
			try
			{
				if (this.WindowState == FormWindowState.Minimized)
				{
					this.Visible = false;
					NotifyIcon.Visible = true;
					NotifyIcon.ShowBalloonTip(1, "Battery Notifier", "Running Minimized", ToolTipIcon.Info);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		//When systemtray icon is clicked
		private void NotifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			try
			{
				this.Visible = true;
				this.WindowState = FormWindowState.Normal;
				NotifyIcon.Visible = false;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}


		private void OpenToolStripMenuItem_Click(object sender, System.EventArgs e)
		{
			try
			{
				this.Visible = true;
				this.WindowState = FormWindowState.Normal;
				NotifyIcon.Visible = false;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void ExitToolStripMenuItem_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		//Set the values of High and Low
		private void NumericUpDownLow_ValueChanged(object sender, EventArgs e)
		{
            Lo = (int)NumericUpDownLow.Value;
			elapsedTime = 0;

		}

		private void NumericUpDownHigh_ValueChanged(object sender, EventArgs e)
		{
			Hi = (int)NumericUpDownHigh.Value;
			elapsedTime = 0;



		}

		private void pingTimer_Tick(object sender, System.EventArgs e)
		{
            Lo = (int)NumericUpDownLow.Value;
            Hi = (int)NumericUpDownHigh.Value;
            BatteryLife = power.BatteryLifePercent * 100;
            // Check the time that has elapsed so far

            //If powercable is plugged in or out, reset the timer
            if (x != (float)power.PowerLineStatus)
			{
				x = (float)power.PowerLineStatus;
				elapsedTime = 0;
                //MessageBox.Show(new Form() { TopMost = true }, "PowerMode Changed");


			}

			if (/*elapsedTime >= 10 && */elapsedTime < elapsedTimeHolding && BatteryLife >= Hi && power.PowerLineStatus == PowerLineStatus.Online)
			{


				elapsedTime = elapsedTimeHolding;
				MessageBox.Show(new Form() { TopMost = true }, "High Battery  " + BatteryLife + " %");
			}
			else if (/*elapsedTime >= 10 && */elapsedTime < elapsedTimeHolding && BatteryLife <= Lo && power.PowerLineStatus == PowerLineStatus.Offline)
			{

				elapsedTime = elapsedTimeHolding;
				MessageBox.Show(new Form() { TopMost = true }, "Low battery  " +  BatteryLife + " %");
			}
			else
			{

				elapsedTime += 1;
                ;
                //Debugging
                
                //Console.WriteLine("X is ");
                //Console.WriteLine(x);
                Console.WriteLine("elapsedTime is ");
                Console.WriteLine(elapsedTime);
                Console.WriteLine("Hi is ");
                Console.WriteLine(Hi);
                //Console.WriteLine("BatteryLifePercent is ");
                //Console.WriteLine("PowerStatus ", (enum)PowerLineStatus);
                //Console.WriteLine(BatteryLifePercent);
				//Console.WriteLine(test.Variable)
                
                /*
                Console.WriteLine("Battery Charge Status: {0}", SystemInformation.PowerStatus.BatteryChargeStatus);
                Console.WriteLine("Battery Full Lifetime: {0}", SystemInformation.PowerStatus.BatteryFullLifetime);
                Console.WriteLine("Battery Life Percent: {0}", SystemInformation.PowerStatus.BatteryLifePercent);
                Console.WriteLine("Battery Life Remaining: {0}", SystemInformation.PowerStatus.BatteryLifeRemaining);
                Console.WriteLine("Power Line Status: {0}", SystemInformation.PowerStatus.PowerLineStatus);
                */

            }
		}



		public static Form1 _DefaultInstance;
		public static Form1 DefaultInstance
		{
			get
			{
				if (_DefaultInstance == null)
					_DefaultInstance = new Form1();

				return _DefaultInstance;
			}
		}
	}
}